package com.itjiange.springbootinit.bizmq;

import com.itjiange.springbootinit.constant.MqConstant;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

/**
 * @author 15117
 * @description 创建消息队列和交换机
 * @data 2023/12/29 9:52
 */
public class MyBIMessageInit {
    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("154.8.205.195");
        factory.setUsername("itjiange");
        factory.setPassword("123456");
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            // 创建交换机
            channel.exchangeDeclare(MqConstant.EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
            // 创建消息队列
            channel.queueDeclare(MqConstant.QUEUE_NAME, false, false, false, null);
            // 绑定消息队列
            channel.queueBind(MqConstant.QUEUE_NAME, MqConstant.EXCHANGE_NAME, MqConstant.ROUTING_KEY);
        }
    }
}
